<h1 id="控件2">控件2</h1>
<p>本章我们继续介绍PyQt5控件。这次的有<code>QPixmap</code>，<code>QLineEdit</code>，<code>QSplitter</code>，和<code>QComboBox</code>。</p>
<h2 id="图片">图片</h2>
<p><code>QPixmap</code>是处理图片的组件。本例中，我们使用<code>QPixmap</code>在窗口里显示一张图片。</p>
<div class="sourceCode" id="cb1"><pre><code class="language-python"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb1-3" data-line-number="3"></a>
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb1-6" data-line-number="6"></a>
<a class="sourceLine" id="cb1-7" data-line-number="7"><span class="co">In this example, we dispay an image</span></a>
<a class="sourceLine" id="cb1-8" data-line-number="8"><span class="co">on the window. </span></a>
<a class="sourceLine" id="cb1-9" data-line-number="9"></a>
<a class="sourceLine" id="cb1-10" data-line-number="10"><span class="co">Author: Jan Bodnar</span></a>
<a class="sourceLine" id="cb1-11" data-line-number="11"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb1-12" data-line-number="12"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb1-13" data-line-number="13"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb1-14" data-line-number="14"></a>
<a class="sourceLine" id="cb1-15" data-line-number="15"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> (QWidget, QHBoxLayout, </a>
<a class="sourceLine" id="cb1-16" data-line-number="16">    QLabel, QApplication)</a>
<a class="sourceLine" id="cb1-17" data-line-number="17"><span class="im">from</span> PyQt5.QtGui <span class="im">import</span> QPixmap</a>
<a class="sourceLine" id="cb1-18" data-line-number="18"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb1-19" data-line-number="19"></a>
<a class="sourceLine" id="cb1-20" data-line-number="20"><span class="kw">class</span> Example(QWidget):</a>
<a class="sourceLine" id="cb1-21" data-line-number="21">    </a>
<a class="sourceLine" id="cb1-22" data-line-number="22">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb1-23" data-line-number="23">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb1-24" data-line-number="24">        </a>
<a class="sourceLine" id="cb1-25" data-line-number="25">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb1-26" data-line-number="26">        </a>
<a class="sourceLine" id="cb1-27" data-line-number="27">        </a>
<a class="sourceLine" id="cb1-28" data-line-number="28">    <span class="kw">def</span> initUI(<span class="va">self</span>):      </a>
<a class="sourceLine" id="cb1-29" data-line-number="29"></a>
<a class="sourceLine" id="cb1-30" data-line-number="30">        hbox <span class="op">=</span> QHBoxLayout(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb1-31" data-line-number="31">        pixmap <span class="op">=</span> QPixmap(<span class="st">&quot;redrock.png&quot;</span>)</a>
<a class="sourceLine" id="cb1-32" data-line-number="32"></a>
<a class="sourceLine" id="cb1-33" data-line-number="33">        lbl <span class="op">=</span> QLabel(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb1-34" data-line-number="34">        lbl.setPixmap(pixmap)</a>
<a class="sourceLine" id="cb1-35" data-line-number="35"></a>
<a class="sourceLine" id="cb1-36" data-line-number="36">        hbox.addWidget(lbl)</a>
<a class="sourceLine" id="cb1-37" data-line-number="37">        <span class="va">self</span>.setLayout(hbox)</a>
<a class="sourceLine" id="cb1-38" data-line-number="38">        </a>
<a class="sourceLine" id="cb1-39" data-line-number="39">        <span class="va">self</span>.move(<span class="dv">300</span>, <span class="dv">200</span>)</a>
<a class="sourceLine" id="cb1-40" data-line-number="40">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;Red Rock&#39;</span>)</a>
<a class="sourceLine" id="cb1-41" data-line-number="41">        <span class="va">self</span>.show()        </a>
<a class="sourceLine" id="cb1-42" data-line-number="42">        </a>
<a class="sourceLine" id="cb1-43" data-line-number="43">        </a>
<a class="sourceLine" id="cb1-44" data-line-number="44"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb1-45" data-line-number="45">    </a>
<a class="sourceLine" id="cb1-46" data-line-number="46">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb1-47" data-line-number="47">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb1-48" data-line-number="48">    sys.exit(app.exec_())</a></code></pre></div>
<pre><code class="language-python">pixmap = QPixmap(&quot;redrock.png&quot;)</code></pre>
<p>创建一个<code>QPixmap</code>对象，接收一个文件作为参数。</p>
<pre><code class="language-python">lbl = QLabel(self)
lbl.setPixmap(pixmap)</code></pre>
<p>把<code>QPixmap</code>实例放到<code>QLabel</code>组件里。</p>
<p>程序展示：</p>
<figure>
<img class="whitelist" src="docs/PyQt5/images/7-pixmap.png" alt="pixmap" />
</figure>
<h2 id="行编辑">行编辑</h2>
<p><code>QLineEdit</code>组件提供了编辑文本的功能，自带了撤销、重做、剪切、粘贴、拖拽等功能。</p>
<div class="sourceCode" id="cb4"><pre><code class="language-python"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3"></a>
<a class="sourceLine" id="cb4-4" data-line-number="4"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb4-5" data-line-number="5"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb4-6" data-line-number="6"></a>
<a class="sourceLine" id="cb4-7" data-line-number="7"><span class="co">This example shows text which </span></a>
<a class="sourceLine" id="cb4-8" data-line-number="8"><span class="co">is entered in a QLineEdit</span></a>
<a class="sourceLine" id="cb4-9" data-line-number="9"><span class="co">in a QLabel widget.</span></a>
<a class="sourceLine" id="cb4-10" data-line-number="10"><span class="co"> </span></a>
<a class="sourceLine" id="cb4-11" data-line-number="11"><span class="co">Author: Jan Bodnar</span></a>
<a class="sourceLine" id="cb4-12" data-line-number="12"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb4-13" data-line-number="13"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb4-14" data-line-number="14"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb4-15" data-line-number="15"></a>
<a class="sourceLine" id="cb4-16" data-line-number="16"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb4-17" data-line-number="17"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> (QWidget, QLabel, </a>
<a class="sourceLine" id="cb4-18" data-line-number="18">    QLineEdit, QApplication)</a>
<a class="sourceLine" id="cb4-19" data-line-number="19"></a>
<a class="sourceLine" id="cb4-20" data-line-number="20"></a>
<a class="sourceLine" id="cb4-21" data-line-number="21"><span class="kw">class</span> Example(QWidget):</a>
<a class="sourceLine" id="cb4-22" data-line-number="22">    </a>
<a class="sourceLine" id="cb4-23" data-line-number="23">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb4-24" data-line-number="24">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb4-25" data-line-number="25">        </a>
<a class="sourceLine" id="cb4-26" data-line-number="26">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb4-27" data-line-number="27">        </a>
<a class="sourceLine" id="cb4-28" data-line-number="28">        </a>
<a class="sourceLine" id="cb4-29" data-line-number="29">    <span class="kw">def</span> initUI(<span class="va">self</span>):      </a>
<a class="sourceLine" id="cb4-30" data-line-number="30"></a>
<a class="sourceLine" id="cb4-31" data-line-number="31">        <span class="va">self</span>.lbl <span class="op">=</span> QLabel(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb4-32" data-line-number="32">        qle <span class="op">=</span> QLineEdit(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb4-33" data-line-number="33">        </a>
<a class="sourceLine" id="cb4-34" data-line-number="34">        qle.move(<span class="dv">60</span>, <span class="dv">100</span>)</a>
<a class="sourceLine" id="cb4-35" data-line-number="35">        <span class="va">self</span>.lbl.move(<span class="dv">60</span>, <span class="dv">40</span>)</a>
<a class="sourceLine" id="cb4-36" data-line-number="36"></a>
<a class="sourceLine" id="cb4-37" data-line-number="37">        qle.textChanged[<span class="bu">str</span>].<span class="ex">connect</span>(<span class="va">self</span>.onChanged)</a>
<a class="sourceLine" id="cb4-38" data-line-number="38">        </a>
<a class="sourceLine" id="cb4-39" data-line-number="39">        <span class="va">self</span>.setGeometry(<span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">280</span>, <span class="dv">170</span>)</a>
<a class="sourceLine" id="cb4-40" data-line-number="40">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;QLineEdit&#39;</span>)</a>
<a class="sourceLine" id="cb4-41" data-line-number="41">        <span class="va">self</span>.show()</a>
<a class="sourceLine" id="cb4-42" data-line-number="42">        </a>
<a class="sourceLine" id="cb4-43" data-line-number="43">        </a>
<a class="sourceLine" id="cb4-44" data-line-number="44">    <span class="kw">def</span> onChanged(<span class="va">self</span>, text):</a>
<a class="sourceLine" id="cb4-45" data-line-number="45">        </a>
<a class="sourceLine" id="cb4-46" data-line-number="46">        <span class="va">self</span>.lbl.setText(text)</a>
<a class="sourceLine" id="cb4-47" data-line-number="47">        <span class="va">self</span>.lbl.adjustSize()        </a>
<a class="sourceLine" id="cb4-48" data-line-number="48">        </a>
<a class="sourceLine" id="cb4-49" data-line-number="49">        </a>
<a class="sourceLine" id="cb4-50" data-line-number="50"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb4-51" data-line-number="51">    </a>
<a class="sourceLine" id="cb4-52" data-line-number="52">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb4-53" data-line-number="53">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb4-54" data-line-number="54">    sys.exit(app.exec_())</a></code></pre></div>
<p>例子中展示了一个编辑组件和一个标签，我们在输入框里键入的文本，会立即在标签里显示出来。</p>
<pre><code class="language-python">qle = QLineEdit(self)</code></pre>
<p>创建一个<code>QLineEdit</code>对象。</p>
<pre><code class="language-python">qle.textChanged[str].connect(self.onChanged)</code></pre>
<p>如果输入框的值有变化，就调用<code>onChanged()</code>方法。</p>
<pre><code class="language-python">def onChanged(self, text):
    
    self.lbl.setText(text)
    self.lbl.adjustSize() </code></pre>
<p>在<code>onChanged()</code>方法内部，我们把文本框里的值赋值给了标签组件，然后调用<code>adjustSize()</code>方法让标签自适应文本内容。</p>
<p>程序展示：</p>
<figure>
<img class="whitelist" src="docs/PyQt5/images/7-qlineedit.png" alt="QLineEdit" />
</figure>
<h2 id="qsplitter">QSplitter</h2>
<p><code>QSplitter</code>组件能让用户通过拖拽分割线的方式改变子窗口大小的组件。本例中我们展示用两个分割线隔开的三个<code>QFrame</code>组件。</p>
<div class="sourceCode" id="cb8"><pre><code class="language-python"><a class="sourceLine" id="cb8-1" data-line-number="1"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb8-3" data-line-number="3"></a>
<a class="sourceLine" id="cb8-4" data-line-number="4"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb8-5" data-line-number="5"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb8-6" data-line-number="6"></a>
<a class="sourceLine" id="cb8-7" data-line-number="7"><span class="co">This example shows</span></a>
<a class="sourceLine" id="cb8-8" data-line-number="8"><span class="co">how to use QSplitter widget.</span></a>
<a class="sourceLine" id="cb8-9" data-line-number="9"><span class="co"> </span></a>
<a class="sourceLine" id="cb8-10" data-line-number="10"><span class="co">Author: Jan Bodnar</span></a>
<a class="sourceLine" id="cb8-11" data-line-number="11"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb8-12" data-line-number="12"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb8-13" data-line-number="13"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb8-14" data-line-number="14"></a>
<a class="sourceLine" id="cb8-15" data-line-number="15"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> (QWidget, QHBoxLayout, QFrame, </a>
<a class="sourceLine" id="cb8-16" data-line-number="16">    QSplitter, QStyleFactory, QApplication)</a>
<a class="sourceLine" id="cb8-17" data-line-number="17"><span class="im">from</span> PyQt5.QtCore <span class="im">import</span> Qt</a>
<a class="sourceLine" id="cb8-18" data-line-number="18"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb8-19" data-line-number="19"></a>
<a class="sourceLine" id="cb8-20" data-line-number="20"><span class="kw">class</span> Example(QWidget):</a>
<a class="sourceLine" id="cb8-21" data-line-number="21">    </a>
<a class="sourceLine" id="cb8-22" data-line-number="22">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb8-23" data-line-number="23">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb8-24" data-line-number="24">        </a>
<a class="sourceLine" id="cb8-25" data-line-number="25">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb8-26" data-line-number="26">        </a>
<a class="sourceLine" id="cb8-27" data-line-number="27">        </a>
<a class="sourceLine" id="cb8-28" data-line-number="28">    <span class="kw">def</span> initUI(<span class="va">self</span>):      </a>
<a class="sourceLine" id="cb8-29" data-line-number="29"></a>
<a class="sourceLine" id="cb8-30" data-line-number="30">        hbox <span class="op">=</span> QHBoxLayout(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb8-31" data-line-number="31"></a>
<a class="sourceLine" id="cb8-32" data-line-number="32">        topleft <span class="op">=</span> QFrame(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb8-33" data-line-number="33">        topleft.setFrameShape(QFrame.StyledPanel)</a>
<a class="sourceLine" id="cb8-34" data-line-number="34"> </a>
<a class="sourceLine" id="cb8-35" data-line-number="35">        topright <span class="op">=</span> QFrame(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb8-36" data-line-number="36">        topright.setFrameShape(QFrame.StyledPanel)</a>
<a class="sourceLine" id="cb8-37" data-line-number="37"></a>
<a class="sourceLine" id="cb8-38" data-line-number="38">        bottom <span class="op">=</span> QFrame(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb8-39" data-line-number="39">        bottom.setFrameShape(QFrame.StyledPanel)</a>
<a class="sourceLine" id="cb8-40" data-line-number="40"></a>
<a class="sourceLine" id="cb8-41" data-line-number="41">        splitter1 <span class="op">=</span> QSplitter(Qt.Horizontal)</a>
<a class="sourceLine" id="cb8-42" data-line-number="42">        splitter1.addWidget(topleft)</a>
<a class="sourceLine" id="cb8-43" data-line-number="43">        splitter1.addWidget(topright)</a>
<a class="sourceLine" id="cb8-44" data-line-number="44"></a>
<a class="sourceLine" id="cb8-45" data-line-number="45">        splitter2 <span class="op">=</span> QSplitter(Qt.Vertical)</a>
<a class="sourceLine" id="cb8-46" data-line-number="46">        splitter2.addWidget(splitter1)</a>
<a class="sourceLine" id="cb8-47" data-line-number="47">        splitter2.addWidget(bottom)</a>
<a class="sourceLine" id="cb8-48" data-line-number="48"></a>
<a class="sourceLine" id="cb8-49" data-line-number="49">        hbox.addWidget(splitter2)</a>
<a class="sourceLine" id="cb8-50" data-line-number="50">        <span class="va">self</span>.setLayout(hbox)</a>
<a class="sourceLine" id="cb8-51" data-line-number="51">        </a>
<a class="sourceLine" id="cb8-52" data-line-number="52">        <span class="va">self</span>.setGeometry(<span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">200</span>)</a>
<a class="sourceLine" id="cb8-53" data-line-number="53">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;QSplitter&#39;</span>)</a>
<a class="sourceLine" id="cb8-54" data-line-number="54">        <span class="va">self</span>.show()</a>
<a class="sourceLine" id="cb8-55" data-line-number="55">        </a>
<a class="sourceLine" id="cb8-56" data-line-number="56">        </a>
<a class="sourceLine" id="cb8-57" data-line-number="57">    <span class="kw">def</span> onChanged(<span class="va">self</span>, text):</a>
<a class="sourceLine" id="cb8-58" data-line-number="58">        </a>
<a class="sourceLine" id="cb8-59" data-line-number="59">        <span class="va">self</span>.lbl.setText(text)</a>
<a class="sourceLine" id="cb8-60" data-line-number="60">        <span class="va">self</span>.lbl.adjustSize()        </a>
<a class="sourceLine" id="cb8-61" data-line-number="61">        </a>
<a class="sourceLine" id="cb8-62" data-line-number="62">        </a>
<a class="sourceLine" id="cb8-63" data-line-number="63"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb8-64" data-line-number="64">    </a>
<a class="sourceLine" id="cb8-65" data-line-number="65">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb8-66" data-line-number="66">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb8-67" data-line-number="67">    sys.exit(app.exec_())</a></code></pre></div>
<p>三个窗口和两个分割线的布局创建完成了，但是要注意，有些主题下，分割线的显示效果不太好。</p>
<pre><code class="language-python">topleft = QFrame(self)
topleft.setFrameShape(QFrame.StyledPanel)</code></pre>
<p>为了更清楚的看到分割线，我们使用了设置好的子窗口样式。</p>
<pre><code class="language-python">splitter1 = QSplitter(Qt.Horizontal)
splitter1.addWidget(topleft)
splitter1.addWidget(topright)</code></pre>
<p>创建一个<code>QSplitter</code>组件，并在里面添加了两个框架。</p>
<pre><code class="language-python">splitter2 = QSplitter(Qt.Vertical)
splitter2.addWidget(splitter1)</code></pre>
<p>也可以在分割线里面再进行分割。</p>
<p>程序展示：</p>
<figure>
<img class="whitelist" src="docs/PyQt5/images/7-qsplitter.png" alt="QSplitter widget" />
</figure>
<h2 id="下拉选框">下拉选框</h2>
<p><code>QComboBox</code>组件能让用户在多个选择项中选择一个。</p>
<div class="sourceCode" id="cb12"><pre><code class="language-python"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="co">#!/usr/bin/python3</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2"><span class="co"># -*- coding: utf-8 -*-</span></a>
<a class="sourceLine" id="cb12-3" data-line-number="3"></a>
<a class="sourceLine" id="cb12-4" data-line-number="4"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb12-5" data-line-number="5"><span class="co">ZetCode PyQt5 tutorial </span></a>
<a class="sourceLine" id="cb12-6" data-line-number="6"></a>
<a class="sourceLine" id="cb12-7" data-line-number="7"><span class="co">This example shows how to use </span></a>
<a class="sourceLine" id="cb12-8" data-line-number="8"><span class="co">a QComboBox widget.</span></a>
<a class="sourceLine" id="cb12-9" data-line-number="9"><span class="co"> </span></a>
<a class="sourceLine" id="cb12-10" data-line-number="10"><span class="co">Author: Jan Bodnar</span></a>
<a class="sourceLine" id="cb12-11" data-line-number="11"><span class="co">Website: zetcode.com </span></a>
<a class="sourceLine" id="cb12-12" data-line-number="12"><span class="co">Last edited: August 2017</span></a>
<a class="sourceLine" id="cb12-13" data-line-number="13"><span class="co">&quot;&quot;&quot;</span></a>
<a class="sourceLine" id="cb12-14" data-line-number="14"></a>
<a class="sourceLine" id="cb12-15" data-line-number="15"><span class="im">from</span> PyQt5.QtWidgets <span class="im">import</span> (QWidget, QLabel, </a>
<a class="sourceLine" id="cb12-16" data-line-number="16">    QComboBox, QApplication)</a>
<a class="sourceLine" id="cb12-17" data-line-number="17"><span class="im">import</span> sys</a>
<a class="sourceLine" id="cb12-18" data-line-number="18"></a>
<a class="sourceLine" id="cb12-19" data-line-number="19"><span class="kw">class</span> Example(QWidget):</a>
<a class="sourceLine" id="cb12-20" data-line-number="20">    </a>
<a class="sourceLine" id="cb12-21" data-line-number="21">    <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>):</a>
<a class="sourceLine" id="cb12-22" data-line-number="22">        <span class="bu">super</span>().<span class="fu">__init__</span>()</a>
<a class="sourceLine" id="cb12-23" data-line-number="23">        </a>
<a class="sourceLine" id="cb12-24" data-line-number="24">        <span class="va">self</span>.initUI()</a>
<a class="sourceLine" id="cb12-25" data-line-number="25">        </a>
<a class="sourceLine" id="cb12-26" data-line-number="26">        </a>
<a class="sourceLine" id="cb12-27" data-line-number="27">    <span class="kw">def</span> initUI(<span class="va">self</span>):      </a>
<a class="sourceLine" id="cb12-28" data-line-number="28"></a>
<a class="sourceLine" id="cb12-29" data-line-number="29">        <span class="va">self</span>.lbl <span class="op">=</span> QLabel(<span class="st">&quot;Ubuntu&quot;</span>, <span class="va">self</span>)</a>
<a class="sourceLine" id="cb12-30" data-line-number="30"></a>
<a class="sourceLine" id="cb12-31" data-line-number="31">        combo <span class="op">=</span> QComboBox(<span class="va">self</span>)</a>
<a class="sourceLine" id="cb12-32" data-line-number="32">        combo.addItem(<span class="st">&quot;Ubuntu&quot;</span>)</a>
<a class="sourceLine" id="cb12-33" data-line-number="33">        combo.addItem(<span class="st">&quot;Mandriva&quot;</span>)</a>
<a class="sourceLine" id="cb12-34" data-line-number="34">        combo.addItem(<span class="st">&quot;Fedora&quot;</span>)</a>
<a class="sourceLine" id="cb12-35" data-line-number="35">        combo.addItem(<span class="st">&quot;Arch&quot;</span>)</a>
<a class="sourceLine" id="cb12-36" data-line-number="36">        combo.addItem(<span class="st">&quot;Gentoo&quot;</span>)</a>
<a class="sourceLine" id="cb12-37" data-line-number="37"></a>
<a class="sourceLine" id="cb12-38" data-line-number="38">        combo.move(<span class="dv">50</span>, <span class="dv">50</span>)</a>
<a class="sourceLine" id="cb12-39" data-line-number="39">        <span class="va">self</span>.lbl.move(<span class="dv">50</span>, <span class="dv">150</span>)</a>
<a class="sourceLine" id="cb12-40" data-line-number="40"></a>
<a class="sourceLine" id="cb12-41" data-line-number="41">        combo.activated[<span class="bu">str</span>].<span class="ex">connect</span>(<span class="va">self</span>.onActivated)        </a>
<a class="sourceLine" id="cb12-42" data-line-number="42">         </a>
<a class="sourceLine" id="cb12-43" data-line-number="43">        <span class="va">self</span>.setGeometry(<span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">300</span>, <span class="dv">200</span>)</a>
<a class="sourceLine" id="cb12-44" data-line-number="44">        <span class="va">self</span>.setWindowTitle(<span class="st">&#39;QComboBox&#39;</span>)</a>
<a class="sourceLine" id="cb12-45" data-line-number="45">        <span class="va">self</span>.show()</a>
<a class="sourceLine" id="cb12-46" data-line-number="46">        </a>
<a class="sourceLine" id="cb12-47" data-line-number="47">        </a>
<a class="sourceLine" id="cb12-48" data-line-number="48">    <span class="kw">def</span> onActivated(<span class="va">self</span>, text):</a>
<a class="sourceLine" id="cb12-49" data-line-number="49">      </a>
<a class="sourceLine" id="cb12-50" data-line-number="50">        <span class="va">self</span>.lbl.setText(text)</a>
<a class="sourceLine" id="cb12-51" data-line-number="51">        <span class="va">self</span>.lbl.adjustSize()  </a>
<a class="sourceLine" id="cb12-52" data-line-number="52">        </a>
<a class="sourceLine" id="cb12-53" data-line-number="53">                </a>
<a class="sourceLine" id="cb12-54" data-line-number="54"><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</a>
<a class="sourceLine" id="cb12-55" data-line-number="55">    </a>
<a class="sourceLine" id="cb12-56" data-line-number="56">    app <span class="op">=</span> QApplication(sys.argv)</a>
<a class="sourceLine" id="cb12-57" data-line-number="57">    ex <span class="op">=</span> Example()</a>
<a class="sourceLine" id="cb12-58" data-line-number="58">    sys.exit(app.exec_())</a></code></pre></div>
<p>本例包含了一个<code>QComboBox</code>和一个<code>QLabel</code>。下拉选择框有五个选项，都是Linux的发行版名称，标签内容为选定的发行版名称。</p>
<pre><code class="language-python">combo = QComboBox(self)
combo.addItem(&quot;Ubuntu&quot;)
combo.addItem(&quot;Mandriva&quot;)
combo.addItem(&quot;Fedora&quot;)
combo.addItem(&quot;Arch&quot;)
combo.addItem(&quot;Gentoo&quot;)</code></pre>
<p>创建一个<code>QComboBox</code>组件和五个选项。</p>
<pre><code class="language-python">combo.activated[str].connect(self.onActivated) </code></pre>
<p>在选中的条目上调用<code>onActivated()</code>方法。</p>
<pre><code class="language-python">def onActivated(self, text):
  
    self.lbl.setText(text)
    self.lbl.adjustSize() </code></pre>
<p>在方法内部，设置标签内容为选定的字符串，然后设置自适应文本大小。</p>
<p>程序展示：</p>
<figure>
<img class="whitelist" src="docs/PyQt5/images/7-qcombobox.png" alt="QComboBox" />
</figure>
